package com.ruoyi.system.controller;

import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.system.domain.SysBusiness;
import com.ruoyi.system.domain.SysEnterpriseCertification;
import com.ruoyi.system.domain.parm.SysEnterpriseCertificationParm;
import com.ruoyi.system.service.ISysBusinessService;
import com.ruoyi.system.service.ISysEnterpriseCertificationService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * 商家企业认证Controller
 *
 * @author ruoyi
 * @date 2024-04-06
 */
@Api(tags = "商家企业认证管理")
@RestController
@RequestMapping("/bus/certification")
public class BusEnterpriseCertificationController extends BaseController {
    @Autowired
    private ISysEnterpriseCertificationService sysEnterpriseCertificationService;

    @Autowired
    private ISysBusinessService sysBusinessService;

    /**
     * 获取商家企业认证详细信息
     */
    @ApiOperation(value = "获取商家企业认证详细信息")
    @GetMapping(value = "/info")
    public AjaxResult getInfo(@RequestParam(name = "id", required = true) String id) throws Exception {
        SysBusiness bu = sysBusinessService.getUserInfo();
        if (bu == null) {
            throw new Exception("用户未登陆");
        }

        return success(sysEnterpriseCertificationService.selectSysEnterpriseCertificationById(id));
    }

    /**
     * 新增商家企业认证
     */
    @ApiOperation(value = "商家企业认证申请")
    @PostMapping(value = "/add")
    public AjaxResult add(@RequestBody @Valid SysEnterpriseCertificationParm sysEnterpriseCertification) throws Exception {

        SysBusiness bu = sysBusinessService.getUserInfo();
        if (bu == null) {
            throw new Exception("用户未登陆");
        }
        List<SysEnterpriseCertification> isHas = sysEnterpriseCertificationService.selectSysEnterpriseCertificationByBusinessId(bu.getId());
        if (isHas.size() > 0) {
            for (SysEnterpriseCertification item : isHas) {
                if(item.getStauts()==1){
                    throw new Exception("已经通过企业认证申请");
                }
                if(item.getStauts()==0){
                    throw new Exception("已经提交企业认证申请");
                }
            }

        }

        sysEnterpriseCertification.setBusinessId(bu.getId());
        sysEnterpriseCertification.setSubmissionTime(new Date());
        sysEnterpriseCertification.setId(UUID.randomUUID().toString());
        return toAjax(sysEnterpriseCertificationService.insertSysEnterpriseCertification(sysEnterpriseCertification));
    }

    /**
     * 修改商家企业认证
     */
    @ApiOperation(value = "商家企业认证申请修改")
    @PutMapping(value = "/edit")
    public AjaxResult edit(@RequestBody @Valid SysEnterpriseCertificationParm sysEnterpriseCertificationParm) throws Exception {

        SysEnterpriseCertification sysEnterpriseCertification = new SysEnterpriseCertification();
        BeanUtils.copyProperties(sysEnterpriseCertificationParm, sysEnterpriseCertification);
        if(sysEnterpriseCertification.getId()==null){
            throw new Exception("id不能为空");

        }
        return toAjax(sysEnterpriseCertificationService.updateSysEnterpriseCertification(sysEnterpriseCertification));
    }

}
